﻿/*
   Author: Tomer Paz
   games.com
   2009
*/

/* popup message handler class */

package monopoly.GameController {
	
	import monopoly.GameController.Dialogs.Templates.MessageBox;
	import monopoly.views.BottomPanel;
	import monopoly.Localization.*;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	
	public class PopupMessageHandler extends Timer
		{
		var myStrings:ILocalLanguage = LocalLanguage.getInstance();
		public const  DEFAULT_MESSAGE:String = myStrings.MsgWaitServer;
		protected var _Message:String = DEFAULT_MESSAGE;
		protected var _MessageBoxContainer:Sprite;
		
		protected var _MessageBox:monopoly.GameController.Dialogs.Templates.MessageBox;
		
		protected var _Timeout:Number = 5000;
		protected var _ProcessFunc:Function = null;
		protected var _Args:Array = null;		// a.k.a. "arguments"
		
		public function PopupMessageHandler(messageBoxContainer:Sprite)
			{
			_MessageBoxContainer = messageBoxContainer;
			_MessageBox = new monopoly.GameController.Dialogs.Templates.MessageBox(_Message);
			_MessageBox.name = BottomPanel.BPANEL_CENTER;

			super(_Timeout, 0);
			}
			
		public function set Message(message:String):void
			{
			_Message = message;
			}
		public function get Message():String
			{
			return _Message;
			}
		public function set Timeout(timeout:Number):void
			{
			_Timeout = timeout;
			super.delay = timeout;
			}
		public function get Timeout():Number
			{
			return super.delay;
			}
		public function get ProcessFunc():Function
			{
			return _ProcessFunc;
			}
		public function get Args():Array
			{
			return _Args;
			}
		public function get MessageBox():Sprite
			{
			return _MessageBox;
			}
		
		/* common message box pop-up functions */
		
		public function popupMessageBox(message:String, processFunc:Function, ... args):void
			{
			if (null != message)
				_MessageBox.Text = message;
			else
				_MessageBox.Text = DEFAULT_MESSAGE;
				
			_MessageBox.visible  = true;
			_ProcessFunc		 = processFunc;
			_Args				 = args;

			_MessageBoxContainer.addChild(_MessageBox);
			
			super.start();
			}
			
		/* two types of events might trigger this function
		   1. Timeout on a popup message timer 
		   2. Deliberate remove of popup message with no timer */
		   
		public function popdownMessageBox():void
			{
			_MessageBox.visible  = false;
			
			if (_MessageBoxContainer.contains(_MessageBox))
				_MessageBoxContainer.removeChild(_MessageBox);
				
			super.reset();
			}
		}
	
}